﻿using System;
using System.Collections.Generic;
using System.Text;
using MeTalker.DBUtility;
using System.Data.SqlClient;
using System.Data;
using AD.Model;

namespace AD.DAL
{
    public abstract class DALBase
    {
        public static SqlDataReader ExecuteReader(string cmdText)
        {
            return SqlHelper.ExecuteReader(cmdText);
        }

        /// <summary>
        /// 相差的天数
        /// </summary>
        /// <param name="nowTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public static int GetDay(DateTime nowTime, DateTime endTime)
        {
            TimeSpan span = nowTime.Subtract(endTime);
            return span.Days;
        }

        /// <summary>
        /// 相差的分钟数
        /// </summary>
        /// <param name="nowTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public static int GetMinutes(DateTime nowTime, DateTime endTime)
        {
            TimeSpan span = nowTime.Subtract(endTime);
            //return 60 + span.Minutes;
            return Math.Abs(span.Minutes);
        }

        public static int GetHours(DateTime nowTime, DateTime endTime)
        {
            TimeSpan span = nowTime.Subtract(endTime);
            //return 60 + span.Minutes;
            return Math.Abs(span.Hours);
        }
        public static readonly DateTime BaseDateTime = DateTime.Parse("1901,1,1");

        protected static void PrepareCommand(SqlCommand cmd, SqlTransaction trans, string cmdText)
        {
            PrepareCommand(cmd, SQL_CONNECT, trans, CommandType.Text, cmdText, null);
        }

        protected static void PrepareCommand(SqlCommand cmd, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
        {
            PrepareCommand(cmd, SQL_CONNECT, trans, CommandType.Text, cmdText, cmdParms);
        }

        protected static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {

            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                {
                    if (parm.Value == null)
                        parm.Value = DBNull.Value;
                    if (parm.Value != null && parm.SqlDbType == SqlDbType.DateTime)
                    {
                        if (BaseDateTime.CompareTo(DateTime.Parse(parm.Value.ToString())) >= 0)
                        {
                            parm.Value = DBNull.Value;
                        }
                    }
                    cmd.Parameters.Add(parm);
                }
            }
        }

        public static readonly SqlConnection SQL_CONNECT = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);

        protected static int ExecuteNonQuery(string sql, SqlParameter[] sqlParams)
        {
            return SqlHelper.ExecuteNonQuery(SQL_CONNECT, CommandType.Text, sql, sqlParams);
        }

        protected static int ExecuteNonQuery(SqlTransaction trans, string cmdText, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteNonQuery(trans, CommandType.Text, cmdText, commandParameters);
        }

        protected static int ExecuteNonQuery(SqlTransaction trans, string cmdText)
        {
            return SqlHelper.ExecuteNonQuery(trans, CommandType.Text, cmdText, null);
        }

        protected static int ExecuteNonQuery(string sql)
        {
            return SqlHelper.ExecuteNonQuery(SQL_CONNECT, CommandType.Text, sql, null);
        }

        protected static DataTable ExecuteDataTable(string sql)
        {
            return ExecuteDataTable(sql, null);
        }

        protected static DataTable ExecuteDataTable(string sql, SqlParameter[] sqlParams)
        {
            return SqlHelper.ExecuteDataTable(sql, sqlParams);
        }
    }
}